<?php
namespace app\material\model;
use app\BaseModel;
use think\facade\Db;



class Folder extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_material_folder';


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        if(!empty($data['pid'])){

            $top = $this->reset($data['pid']);

            $count = count($top);

            if($count>5){

                $this->errorMsg('文件夹最多5级');
            }
        }


        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-29 14:13
     * @功能说明:企业文件夹列表
     */

    public function companyDataList($dis,$page=10){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['create_time'] = date('m-d H:i',$v['create_time']);

                $v['update_time'] = date('m-d H:i',$v['update_time']);
                //文件数量
                $v['file_count']  = $this->getUnderFileCount($v['id']);

            }
        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-29 14:21
     * @功能说明:获取文件夹下面文件的数量
     */
    public function getUnderFileCount($id,$map=[]){

        $file_model = new File();

        $ids = $this->getSon($id);

        $ids[] = $id;

        $dis = [

            'status' => 1,
        ];


        $data = $file_model->where($dis)->where('folder_id','in',$ids)->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->count();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-29 14:19
     * @功能说明:
     *
     */
    public function getSon($id,&$data=[]){

        $list = $this->where(['pid'=>$id])->where('status','=',1)->order('top desc,id desc')->column('id');

        $data = array_merge($list,$data);

        if(!empty($list)){

            foreach ($list as $value){

                $this->getSon($value,$data);

            }

        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-30 09:45
     * @功能说明：递归查询下级的id
     */
    public function getLowId($id,&$data=array()){

        $cate = $this->where(['pid'=>$id])->where('status','>',-1)->column('id');

        $data = array_merge($data,$cate);

        if(!empty($cate)){

            foreach ($cate as $value){

                $this->getLowId($value,$data);

            }

        }

        return array_values($data);

    }


    /**
     * @param $id
     * 回显
     */
    public function reset($id,&$data=array()){

        $cate = $this->where(['id'=>$id])->where('status','>',-1)->find();

        $cate = !empty($cate)?$cate->toArray():'';

        if(!empty($cate)&&$cate['pid']!=$cate['id']){

            array_unshift($data,$cate['id']);

            $this->reset($cate['pid'],$data);
        }

        return array_values($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis){

        $data = $this->where($dis)->order('top desc,id desc')->select()->toArray();

        return $this->getTree($data,0);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataPageList($dis,$page=10){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $child = $this->getChildren($v['id']);

                if(!empty($child)){

                    $child = $this->getTree($child,$v['id']);

                    $v['children'] = $child;

                }

            }

        }

        return $data;

    }

    /**
     * @param $data
     * @param $pId
     * @return array
     * 递归无限极
     */
    public function getTree($data, $pId){
        $tree = array();
        if(!empty($data)){
            foreach($data as $k => $v) {
                if($v['pid'] == $pId) {
                    $v['children'] = $this->getTree($data, $v['id']);
                    if(empty($v['children'])){
                        unset($v['children']);
                    }
                    $tree[]   = $v;
                }
            }
        }
        return $tree;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-28 14:54
     * @功能说明:获取下级
     */
    public function getChildren($id,&$data=[]){

        $list = $this->where(['pid'=>$id])->where('status','>',-1)->order('top desc,id desc')->select()->toArray();

        $data = array_merge($list,$data);

        if(!empty($list)){

            foreach ($list as $value){

                $this->getChildren($value['id'],$data);

            }

        }

        return $data;

    }



}